package com.sheliduan.itlearning.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sheliduan.itlearning.common.domain.dos.ArticleCourseDO;
import com.sheliduan.itlearning.common.domain.dos.VideoCourseDO;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author: 断舍离
 * @date: 2025/2/10 10:15
 * @description: 视频课程映射接口
 **/
public interface VideoCourseMapper extends BaseMapper<VideoCourseDO> {

    /*
     * 根据查询条件分页查询课程数据
     * */
    default Page<VideoCourseDO> selectPageList(long current, long size, String title, Integer userId, LocalDateTime startDate, LocalDateTime endDate){
        // MyBatis分页对象, 需传入当前页码和每页记录数
        Page<VideoCourseDO> page = new Page<>(current, size);
        LambdaQueryWrapper<VideoCourseDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(title), VideoCourseDO::getTitle, title)
                .eq(VideoCourseDO::getUserId, userId)
                .ge(Objects.nonNull(startDate), VideoCourseDO::getCreateTime, startDate) // 大于等于 startDate
                .le(Objects.nonNull(endDate), VideoCourseDO::getCreateTime, endDate) // 小于等于 endDate
                .orderByDesc(VideoCourseDO::getCreateTime);
        return selectPage(page, wrapper);
    }

    /*
     * 根据查询条件分页查询课程数据(管理员用, 区别是不查看草稿状态的课程)
     * */
    default Page<VideoCourseDO> selectPageListByAdmin(long current, long size, String title, LocalDateTime startDate, LocalDateTime endDate){
        // MyBatis分页对象, 需传入当前页码和每页记录数
        Page<VideoCourseDO> page = new Page<>(current, size);
        LambdaQueryWrapper<VideoCourseDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(title), VideoCourseDO::getTitle, title)
                .ne(VideoCourseDO::getStatus, "0")
                .ge(Objects.nonNull(startDate), VideoCourseDO::getCreateTime, startDate) // 大于等于 startDate
                .le(Objects.nonNull(endDate), VideoCourseDO::getCreateTime, endDate) // 小于等于 endDate
                .orderByDesc(VideoCourseDO::getCreateTime);
        return selectPage(page, wrapper);
    }

    /*
    * 修改课程
    * */
    default int updateCourse(VideoCourseDO videoCourseDO) {
        LambdaUpdateWrapper<VideoCourseDO> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(VideoCourseDO::getTitle, videoCourseDO.getTitle());
        wrapper.set(VideoCourseDO::getCover, videoCourseDO.getCover());
        wrapper.set(VideoCourseDO::getSummary, videoCourseDO.getSummary());
        wrapper.set(VideoCourseDO::getLocation, videoCourseDO.getLocation());
        wrapper.set(VideoCourseDO::getPrice, videoCourseDO.getPrice());
        wrapper.set(VideoCourseDO::getUpdateTime, videoCourseDO.getUpdateTime());
        wrapper.eq(VideoCourseDO::getId, videoCourseDO.getId());
        return update(null, wrapper);
    }

    /*
    * 投稿课程时, 更改课程状态
    * */
    default int updateCourseStatus(Integer courseId, Integer status) {
        LambdaUpdateWrapper<VideoCourseDO> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(VideoCourseDO::getStatus, status);
        wrapper.eq(VideoCourseDO::getId, courseId);
        return update(null, wrapper);
    }
}
